package io.github.talelin.latticy.controller.v1.K;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.K.CollectorDTO;
import io.github.talelin.latticy.model.K.CollectordwmcDO;
import io.github.talelin.latticy.model.K.CollectorwgmcDO;
import io.github.talelin.latticy.model.K.CollectorDO;
import io.github.talelin.latticy.service.K.CollectorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;
import java.util.List;
/**
 * @author generator@TaleLin
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/v1/Collector")
@Validated
public class CollectorController {

    @Autowired
    private CollectorService CollectorService;

    @GetMapping("/{id}")
    public CollectorDO getGateway(@PathVariable(value = "id")
                                 @Positive(message = "{id.positive}") Integer id) {
        CollectorDO b_gateway = CollectorService.getById(id);
        if (b_gateway == null) {
            throw new NotFoundException("b_gateway not found", 10022);
        }
        return b_gateway;
    }

    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated CollectorDTO dto) {
        CollectorDO CollectorDO = new CollectorDO();
        BeanUtils.copyProperties(dto,CollectorDO);
        this.CollectorService.save(CollectorDO);
        return new CreatedVO();
    }


    @PutMapping("/{id}")
    @PermissionMeta("更新")
    @GroupRequired
    public UpdatedVO update(@RequestBody @Validated CollectorDTO dto,
                            @PathVariable @Positive(message = "{id.positive}") Long id) {
        CollectorService.update(dto, id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable
                            @Positive(message = "{id.positive}") Long id) {
        CollectorService.delete(id);
        return new DeletedVO();
    }

    @GetMapping("/page")
    public PageResponseVO<CollectorDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page
    ) {
        Page<CollectorDO> pager = new Page<>(page, count);
        IPage<CollectorDO> paging = CollectorService.getBaseMapper().selectPage(pager, null);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<CollectorDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword
    ) {
        Page<CollectorDO> pager = new Page<>(page, count);
        IPage<CollectorDO> paging = CollectorService.seachCollector(pager,keyword);
        return PageUtil.build(paging);
    }

    @GetMapping("/getCollector")
    @LoginRequired
    public List<CollectordwmcDO> getCollector() {
        List<CollectordwmcDO> getCollectors = this.CollectorService.getCollector();
        return getCollectors;
    }

    @GetMapping("/getCollector1")
    @LoginRequired
    public List<CollectorwgmcDO> getCollector1() {
        List<CollectorwgmcDO> getCollectors = this.CollectorService.getCollector1();
        return getCollectors;
    }
}
